# Definition for a binary tree node.
class TreeNode(object):
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None

class Solution(object):
    def zigzagLevelOrder(self, root):
        """
        :type root: TreeNode
        :rtype: List[List[int]]
        """
        reault = []
        queue = [root] if root else []
        toRight = True
        while queue:
            newQueue = []
            ret = []
            for root in queue:
                ret.append(root.val)
                if root.left: newQueue.append(root.left)
                if root.right: newQueue.append(root.right)
            if not toRight: ret.reverse()
            toRight = not toRight
            queue = newQueue
            if ret: reault.append(ret)
        return reault

